/*************************************************************************
	> File Name: some_algorithm.cc
	> Author: likang
	> Mail:  
	> Created Time: 2014年12月23日 星期二 11时22分07秒
 ************************************************************************/

#include<iostream>
#include<algorithm>
#include<numeric>
#include<string>
#include<vector>
#include<list>
#include<iterator>          //back_inserter
using namespace std;

int main(void) {
	string s[] = {"I","Love","You"};
	vector<string> svec(s,s+3);
	/*累加容器元素算法，类型为第三个形参的类型，因此在字符串容器的累加中，
	 * 第三个形参必须是string类型，不能是字符串字面值*/
	string sum = accumulate(svec.begin(),svec.end(),string("sum:"));
	cout << sum << endl;

	/*find  查找元素是否存在*/
	vector<string>::const_iterator result = find(svec.begin(),svec.end(),string("Love"));
	if (result == svec.end()) 
		cout << "not exits" << endl;
	else
		cout <<"exit,from begin:" << result-svec.begin() << endl;

	/*find_first_of 查找两个容器的相同元素*/
	string s1[] = {"I","Love","Him"};
	list<string> slist(s1,s1+3);
	list<string>::iterator it = slist.begin();
	int common_ele = 0;
	vector<string> common;
	while ((it = find_first_of(it,slist.end(),svec.begin(),svec.end())) != slist.end()) {
		++common_ele;
		common.push_back(*it);
		++it;

	}
	cout << "svec and slist have " << common_ele << " elements!" << endl;
	for (int i = 0; i < common_ele; ++i) {
		cout << common[i] << "\t";
	}
	cout << endl;

	/*fill_n usage*/
	vector<int> ivec(2,0);
	fill_n(back_inserter(ivec),10,1);
	for (vector<int>::iterator itt = ivec.begin(); itt != ivec.end(); ++itt) {
		cout << *itt << " ";
	}
	cout << endl;
	/*replace usage*/
	replace(ivec.begin(),ivec.end(),1,2);
	for (vector<int>::iterator itt = ivec.begin(); itt != ivec.end(); ++itt) {
		cout << *itt << " ";
	}
	cout << endl;
	/*replace_copy usage*/
	list<int> ilist(12,9);
	replace_copy(ivec.begin(),ivec.end(),back_inserter(ilist),2,1);
	for (list<int>::iterator itt = ilist.begin(); itt != ilist.end(); ++itt) {
		cout << *itt << " ";
	}
	cout << endl;
	return 0;
}
